package com.huihuiyun.code;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Vector;

public class GeneralCodeUtil {
    public void copyAndRenameTemplateWebsiteBookDemo(String templatePath, String outputPath, String name, String chineseName) throws IOException {
        List<String> newLines = new Vector<>();

        copyAndReplace(templatePath, outputPath, "/src/models/", "modelBook.js", name);
        copyAndReplace(templatePath, outputPath, "/src/services/", "serviceBook.js", name);
        copyAndReplace(templatePath, outputPath, "/mock/", "mockBook.js", name);

        copyAndReplaceDir(templatePath + "/src/routes/PageBook", outputPath + "/src/routes/PageBook", name, chineseName);

        newLines = getRoadHogImport(templatePath + "/.roadhogrc.mock.js", name);
        replaceSome(outputPath + "/.roadhogrc.mock.js", "import-" + name, "/* new import */", newLines);

        newLines = printRoadHogUrl(templatePath + "/.roadhogrc.mock.js", name);
        replaceSome(outputPath + "/.roadhogrc.mock.js", "url-" + name, "/* new url */", newLines);

        newLines = getMenu(templatePath + "/src/common/menu.js", name, chineseName);
        replaceSome(outputPath + "/src/common/menu.js", "menu-" + name, "/* new menu */", newLines);

        newLines = getRouteLines(templatePath + "/src/common/router.js", name);
        replaceSome(outputPath + "/src/common/router.js", "route-" + name, "/* new route */", newLines);
        // view object
        newLines = new Vector<>();
        List<String> hhyBookVOLines = FileUtils.readLines(new File(templatePath + "/src/viewobjects/HhyBookVO.js"));
        for (String line : hhyBookVOLines) {
            line = replaceLine(line, name, chineseName);
            newLines.add(line);
        }
        FileUtils.writeLines(new File(outputPath + "/src/viewobjects/Hhy" + getBigName(name) + "VO.js"), newLines);
    }

    public void copyAndRenameTemplateWebsiteBookDemoServer(String serverPath, String outputServerPath, String domainName, String chineseName, String role) throws IOException {
        //eg:
        //template/website/viewobjects/HhyBookVO.java
        //seller/website/viewobjects/HhyOrderVO.java
        String fromFile = "/viewobjects/HhyBookVO.java";
        String line = FileUtils.readFileToString(new File(serverPath + fromFile));
        line = replaceLine(line, domainName, chineseName, role);
        String outputFileName = replaceLine(fromFile, domainName, chineseName, role);
        FileUtils.writeStringToFile(new File(outputServerPath + outputFileName), line);

        //eg:
        //template/website/TemplateWebsiteBookController.java
        //seller/website/TemplateWebsiteOrderController.java
        fromFile = "/TemplateWebsiteBookController.java";
        line = FileUtils.readFileToString(new File(serverPath + fromFile));
        line = replaceLine(line, domainName, chineseName, role);
        outputFileName = replaceLine(fromFile, domainName, chineseName, role);
        FileUtils.writeStringToFile(new File(outputServerPath + outputFileName), line);
    }

    private void copyAndReplaceDir(String fromDir, String toDir, String name, String chineseName) throws IOException {
        File fromDirFile = new File(fromDir);
        toDir = replaceLine(toDir, name);
        File toDirFile = new File(toDir);
        if (!toDirFile.exists()) {
            toDirFile.mkdirs();
        }

        for (File file : fromDirFile.listFiles()) {
            String fileName = file.getName();
            if (file.isFile()) {

                String fileNameNew = replaceLine(fileName, name);
                String toFile = toDir + "/" + fileNameNew;
                String line = FileUtils.readFileToString(file);
                line = replaceLine(line, name, chineseName);
                FileUtils.writeStringToFile(new File(toFile), line);
            } else {
                copyAndReplaceDir(fromDir + "/" + fileName, toDir + "/" + fileName, name, chineseName);
            }
        }
    }

    public static List<String> readLinesByTag(String path, String tagStart, String tagEnd) throws IOException {
        List<String> lines = FileUtils.readLines(new File(path));
        List<String> outLines = new Vector<>();
        boolean isStart = false;
        for (String line : lines) {
            if (isStart) {
                if (line.indexOf(tagEnd) >= 0) {
                    break;
                } else {
                    outLines.add(line);
                }
            } else {
                if (line.indexOf(tagStart) >= 0) {
                    isStart = true;
                }
            }
        }
        return outLines;
    }

    public void replaceSome(String path, String name, String tag, List<String> newLines) throws IOException {
        String tagStart = "/* start " + name + " */";
        String tagEnd = "/* end " + name + " */";
        List<String> lines = FileUtils.readLines(new File(path));
        List<String> outLines = new Vector<>();
        boolean isStart = false;
        boolean isFinish = false;
        for (String line : lines) {
            if (isStart) {
                if (line.indexOf(tagEnd) >= 0) {
                    isStart = false;
                    isFinish = true;
                }
            } else {
                if (line.indexOf(tagStart) >= 0) {
                    isStart = true;
                    outLines.add(tagStart);
                    outLines.addAll(newLines);
                    outLines.add(tagEnd);
                } else {
                    outLines.add(line);
                }
            }
        }
        if (!isFinish) {
            outLines = new Vector<>();
            for (String line : lines) {
                if (line.indexOf(tag) >= 0) {
                    outLines.add(line);
                    outLines.add(tagStart);
                    outLines.addAll(newLines);
                    outLines.add(tagEnd);
                } else {
                    outLines.add(line);
                }
            }
        }
        FileUtils.writeLines(new File(path), outLines);
    }

    private static String replaceLine(String line, String name) {
        return replaceLine(line, name, null, null);
    }

    private static String replaceLine(String line, String name, String chineseName) {
        return replaceLine(line, name, chineseName, null);
    }

    private static String replaceLine(String line, String name, String chineseName, String role) {

        line = line.replace("book", name);
        String bigName = getBigName(name);
        if (bigName != null) {
            line = line.replace("Book", bigName);
        }
        if (role != null) {
            line = line.replace("template", role);
        }
        String bigRole = getBigName(role);
        if (bigRole != null) {
            line = line.replace("Template", bigRole);
        }
        if (chineseName != null) {
            line = line.replace("书籍", chineseName);
        }
        return line;
    }

    private List<String> getMenu(String template, String name, String chinese) throws IOException {
        List<String> lines = readLinesByTag(template, "/* start menu-book */", "/* end menu-book */");
        List<String> newLines = new Vector();
        for (String line : lines) {
            line = replaceLine(line, name, chinese);
            newLines.add(line);
        }
        return newLines;
    }

    private List<String> getRouteLines(String template, String name) throws IOException {
        System.out.println("===================");
        System.out.println("route");
        System.out.println("===================");

        List<String> lines = readLinesByTag(template, "/* start route-book */", "/* end route-book */");
        List<String> newLines = new Vector();
        for (String line : lines) {
            line = replaceLine(line, name);
            newLines.add(line);
        }
        return newLines;

    }

    private static void copyAndReplace(String fromPath, String toPath, String path, String fromName, String name) throws IOException {
        List<String> bookLines = FileUtils.readLines(new File(fromPath + path + fromName));

        List<String> newLines = new Vector<>();
        for (String line : bookLines) {
            line = replaceLine(line, name);
            newLines.add(line);
        }
        FileUtils.writeLines(new File(toPath + path + "/" + replaceLine(fromName, name)), newLines);
    }

    private static List<String> getRoadHogImport(String template, String name) throws IOException {
        System.out.println("===================");
        System.out.println("route import ");
        System.out.println("===================");

        List<String> lines = readLinesByTag(template, "/* start import-book */", "/* end import-book */");
        List<String> newLines = new Vector();
        for (String line : lines) {
            line = replaceLine(line, name);
            newLines.add(line);
        }
        return newLines;
    }

    private static List<String> printRoadHogUrl(String template, String name) throws IOException {
        System.out.println("===================");
        System.out.println("roadhog");
        System.out.println("===================");

        List<String> lines = readLinesByTag(template, "/* start url-book */", "/* end url-book */");
        List<String> newLines = new Vector();
        for (String line : lines) {
            line = replaceLine(line, name);
            newLines.add(line);
        }
        return newLines;
    }

    private static String getBigName(String domainName) {
        if (domainName == null) {
            return null;
        }
        String bigDomain = domainName.substring(0, 1).toUpperCase() + domainName.substring(1);
        return bigDomain;
    }

    public static void export2JsVO(String javaDomainPath, String jsDomainPath, String domainName) throws IOException {
        List<String> domainLines = FileUtils.readLines(new File(javaDomainPath + "/" + domainName + ".java"));
        List<String> newLines = new Vector<>();
        for (String line : domainLines) {
            if (line.startsWith("import ")) {
                continue;
            }
            if (line.startsWith("package ")) {
                continue;
            }

            if (line.indexOf("@Entity") >= 0) {
                continue;
            }
            if (line.indexOf("@") >= 0) {
                continue;
            }
            if (line.indexOf("@Id") >= 0) {
                continue;
            }
            if (line.indexOf("shareRate") >= 0) {
                line = "    shareRate;";
            }
            if (line.indexOf(domainName) >= 0) {
                line = line.replace(domainName, domainName + "VO");
            }
            if (line.indexOf("new BigDecimal") > 0) {
                line = line.substring(0, line.indexOf("=")) + ";";
            }
            line = line.replaceAll("final ", "");
            line = line.replaceAll("public ", "");
            line = line.replaceAll("String ", "");
            line = line.replaceAll(" int", "");
            line = line.replaceAll("BigDecimal ", "");
            line = line.replaceAll("Date ", "");
            line = line.replaceAll("long ", "");
            line = line.replaceAll("byte ", "");
            newLines.add(line);
        }
        newLines.add(String.format("export {%s};", domainName + "VO"));
        FileUtils.writeLines(new File(jsDomainPath + "/" + domainName + "VO.js"), newLines);
    }

    public static void newViewObject(String javaDomainPath, String javaDomainVOPath, String domainName, String domainVOName, String packageName) throws IOException {
        List<String> domainLines = FileUtils.readLines(new File(javaDomainPath + "/" + domainName + ".java"));
        List<String> newLines = new Vector<>();
        for (String line : domainLines) {
            if (line.indexOf("@Entity") >= 0) {
                continue;
            }
            if (line.indexOf("@Table") >= 0) {
                continue;
            }
            if (line.indexOf("@Id") >= 0) {
                continue;
            }
            if (line.indexOf("javax.persistence") >= 0) {
                continue;
            }
            if (line.indexOf("package") >= 0) {
                line = line.replace("server.domain", packageName);
            }

            if (line.indexOf(domainName) >= 0) {
                line = line.replace(domainName, domainVOName);
            }
            newLines.add(line);
        }
        FileUtils.writeLines(new File(javaDomainVOPath + "/" + domainVOName + ".java"), newLines);
    }

    public static void newService(String servicePath, String domainName, String smallDomain) throws IOException {

        List<String> bookServiceLines = FileUtils.readLines(new File(servicePath + "BookService.java"));
        List<String> newServiceLines = new Vector<>();
        for (String line : bookServiceLines) {
            line = line.replaceAll("book", smallDomain);
            line = line.replaceAll("Book", domainName);
            newServiceLines.add(line);
        }
        String serviceFileName = servicePath + domainName + "Service.java";
        FileUtils.writeLines(new File(serviceFileName), newServiceLines);
    }

    public static void newRepository(String repositoryPath, String domainName, String smallDomain) throws IOException {
        List<String> bookRepositoryLines = FileUtils.readLines(new File(repositoryPath + "BookRepository.java"));
        List<String> newRepositoryLines = new Vector<>();
        for (String line : bookRepositoryLines) {
            line = line.replaceAll("book", smallDomain);
            line = line.replaceAll("Book", domainName);
            newRepositoryLines.add(line);
        }
        String repositoryFileName = repositoryPath + domainName + "Repository.java";
        FileUtils.writeLines(new File(repositoryFileName), newRepositoryLines);
    }
}
